#include<stdio.h>

typedef struct ListNode {
	int val;
	struct ListNode* next;
	
}LTN;


struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
	if (list1 == NULL && list2 == NULL)
	{
		return NULL;
	}
	if (list1 == NULL && list2 != NULL)
	{
		return list2;
	}
	if (list2 == NULL && list1 != NULL)
	{
		return list1;
	}
	LTN* l1 = list1;
	LTN* l2 = list2;
	LTN* newhead = NULL;
	LTN* newtail = NULL;
	while (l1 && l2)
	{
		if (l1->val < l2->val)
		{
			if (newhead == NULL)
			{
				newhead = newtail = l1;
			}
			else {
				newtail->next = l1;
				newtail = newtail->next;
			}
			l1 = l1->next;
		}
		else {
			if (newhead == NULL)
			{
				newhead = newtail = l2;
			}
			else {
				newtail->next = l2;
				newtail = newtail->next;
			}
			l2 = l2->next;
		}
	}
	if (l2)
		newtail->next = l2;
	if (l1)
		newtail->next = l1;
	return newhead;

}